package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.AlipayInfo;
import com.chain.BrotherExchange.data.model.BankCardInfo;
import com.chain.BrotherExchange.data.model.BaseResponseModel;
import com.chain.BrotherExchange.data.model.PayPalInfo;
import com.chain.BrotherExchange.data.model.UserPayment;
import com.chain.BrotherExchange.data.model.WeChatPayInfo;
import com.chain.BrotherExchange.utils.Androids;

/**
 * 支付方式
 */
public class PaymentViewModel extends AndroidViewModel {

    private int uid = Androids.LocalConfig.getUid();
    private String token = Androids.LocalConfig.getToken();

    private MutableLiveData<Resource<String>> mSendMsmLiveData;
    private MutableLiveData<PayPalInfo.DataBean.PaypalPayBean> mPayPalInfoLiveData;
    private MutableLiveData<AlipayInfo.DataBean.AliPayBean> mAlipayInfoLiveData;
    private MutableLiveData<BankCardInfo.DataBean.BankPayBean> mBankCardInfoLiveData;
    private MutableLiveData<WeChatPayInfo.DataBean.WechatPayBean> mWeChatPayInfoLiveData;
    private MutableLiveData<Resource<BaseResponseModel>> mSubmitPaymentLiveData;
    private MutableLiveData<Resource<UserPayment.DataBean>> mUserPaymentLiveData;

    public PaymentViewModel(@NonNull Application application) {
        super(application);
        mSendMsmLiveData = new MutableLiveData<>();
        mUserPaymentLiveData = new MutableLiveData<>();
        mSubmitPaymentLiveData = new MutableLiveData<>();
        mPayPalInfoLiveData = new MutableLiveData<>();
        mAlipayInfoLiveData = new MutableLiveData<>();
        mBankCardInfoLiveData = new MutableLiveData<>();
        mWeChatPayInfoLiveData = new MutableLiveData<>();
    }

    public MutableLiveData<Resource<UserPayment.DataBean>> userPayment() {
         int uid = Androids.LocalConfig.getUid();
         String token = Androids.LocalConfig.getToken();
        mUserPaymentLiveData.setValue(Resource.loading());
        DataRepository.get().userPayment(uid, token, result -> {
            if (result.isSucceed()) {
                mUserPaymentLiveData.setValue(Resource.success(result.getData()));
            } else {
                mUserPaymentLiveData.setValue(Resource.error(result.getMsg()));
            }
        });
        return mUserPaymentLiveData;
    }

    public MutableLiveData<Resource<String>> sendMessage(int type) {
        mSendMsmLiveData.setValue(Resource.loading());
        DataRepository.get().sendMessage(
                Androids.LocalConfig.getRegion(), Androids.LocalConfig.getPhone(), type, result -> {
                    if (result.isSucceed()) {
                        mSendMsmLiveData.setValue(Resource.success(result.getMsg()));
                    } else {
                        mSendMsmLiveData.setValue(Resource.error(result.getMsg()));
                    }
                });

        return mSendMsmLiveData;
    }

    // 设置银行卡支付
    public MutableLiveData<Resource<BaseResponseModel>> setBankPayment(
            String name, String bankName, String bankAddress, String bankNumber,
            String confirmNumber, String code) {
        mSubmitPaymentLiveData.setValue(Resource.loading());
        DataRepository.get().setBankPayment(uid, token, name, bankName, bankAddress, bankNumber,
                confirmNumber, code, result -> {
                    if (result.isSucceed()) {
                        mSubmitPaymentLiveData.setValue(Resource.success(result.getMsg()));
                    } else {
                        mSubmitPaymentLiveData.setValue(Resource.error(result.getMsg()));
                    }
                });
        return mSubmitPaymentLiveData;
    }


    // 设置支付宝支付
    public MutableLiveData<Resource<BaseResponseModel>> setAliPayPayment(
            String name, String aliPay, String rqCode, String code) {
        mSubmitPaymentLiveData.setValue(Resource.loading());
        DataRepository.get().setAliPayPayment(uid, token, name, aliPay, rqCode, code, result -> {
            if (result.isSucceed()) {
                mSubmitPaymentLiveData.setValue(Resource.success(result.getMsg()));
            } else {
                mSubmitPaymentLiveData.setValue(Resource.error(result.getMsg()));
            }
        });
        return mSubmitPaymentLiveData;
    }

    // 设置微信支付
    public MutableLiveData<Resource<BaseResponseModel>> setWeChatPayment(
            String name, String weChat, String rqCode, String code) {
        mSubmitPaymentLiveData.setValue(Resource.loading());
        DataRepository.get().setWeChatPayment(uid, token, name, weChat, rqCode, code, result -> {
            if (result.isSucceed()) {
                mSubmitPaymentLiveData.setValue(Resource.success(result.getMsg()));
            } else {
                mSubmitPaymentLiveData.setValue(Resource.error(result.getMsg()));
            }
        });
        return mSubmitPaymentLiveData;
    }

    // 添加payPal支付
    public MutableLiveData<Resource<BaseResponseModel>> setPayPalPayment(String name, String code) {
        mSubmitPaymentLiveData.setValue(Resource.loading());
        DataRepository.get().setPayPalPayment(uid, token, name, code, result -> {
            if (result.isSucceed()) {
                mSubmitPaymentLiveData.setValue(Resource.success(result.getMsg()));
            } else {
                mSubmitPaymentLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mSubmitPaymentLiveData;
    }

    // 银行卡信息
    public MutableLiveData<BankCardInfo.DataBean.BankPayBean> bankCardInfo() {
        DataRepository.get().bankCardInfo(uid, token, result -> {
            if (result.isSucceed()) {
                mBankCardInfoLiveData.setValue(result.getData().getBank_pay());
            }
        });
        return mBankCardInfoLiveData;
    }

    // 支付宝信息
    public MutableLiveData<AlipayInfo.DataBean.AliPayBean> aliPayInfo() {
        DataRepository.get().aliPayInfo(uid, token, result -> {
            if (result.isSucceed()) {
                mAlipayInfoLiveData.setValue(result.getData().getAli_pay());
            }
        });
        return mAlipayInfoLiveData;
    }

    // 微信信息
    public MutableLiveData<WeChatPayInfo.DataBean.WechatPayBean> weChatPayInfo() {
        DataRepository.get().weChatPayInfo(uid, token, result -> {
            if (result.isSucceed()) {
                mWeChatPayInfoLiveData.setValue(result.getData().getWechat_pay());
            }
        });
        return mWeChatPayInfoLiveData;
    }

    // payPal信息
    public MutableLiveData<PayPalInfo.DataBean.PaypalPayBean> payPalInfo() {
        DataRepository.get().payPalInfo(uid, token, result -> {
            if (result.isSucceed()) {
                mPayPalInfoLiveData.setValue(result.getData().getPaypal_pay());
            }
        });
        return mPayPalInfoLiveData;
    }
}
